package com.example.user.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.commons.analysis.ApiParse;
import com.example.commons.annotation.DebounceLock;
import com.example.commons.exception.BusinessException;
import com.example.commons.inspect.ParamsInspect;
import com.example.commons.time.TimeTools;
import com.example.commons.vo.UserVo;
import com.example.user.config.CustomThreadPoolExecutor;
import com.example.user.config.ThreadPoolUtil;
import com.example.user.entity.po.UserDo;
import com.example.user.mapper.UserMapper;
import com.example.user.service.UsersService;
import com.example.user.service.feign.UserAccountClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Function;

@Service
@Slf4j
public class UsersServiceImpl  extends ServiceImpl<UserMapper,UserDo> implements UsersService  {
    @Autowired
    UserMapper userMapper;
    @Autowired
    PasswordEncoder passwordEncoder;
    @Autowired
    UserAccountClient userAccountService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void register(String username, String password) {
        ParamsInspect.IsNull(username, "账号");
        ParamsInspect.IsNull(password, "密码");
        String id = String.valueOf(System.currentTimeMillis());
        String registerTime = TimeTools.getTime();
//      password = BCrypt.hashpw(password, BCrypt.gensalt());
        password = passwordEncoder.encode(password);
        if (1 != userMapper.register(id, username, password, registerTime)) {
            throw new BusinessException("新增失败");
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("userId", id);
        //远程调用，注册账户
        ApiParse.parse(userAccountService.addAccount(jsonObject.toJSONString()));
    }

    @Override
    public UserDo query(UserVo userVo) {
        return userMapper.selectById(userVo.getId());
    }

    public void test1() throws ExecutionException, InterruptedException {
        List<CompletableFuture<Void>> futures = new ArrayList<>();
        CustomThreadPoolExecutor executor = ThreadPoolUtil.getExecutor();
       for(int i=1;i<=10;i++){
           AtomicInteger atomicInteger = new AtomicInteger(i);
           CompletableFuture<Void> future = CompletableFuture.runAsync(()->{

               int a  =get(atomicInteger);
               test3(a);

           },executor);

           futures.add(future) ;
       }
        CompletableFuture[] arr =  futures.toArray(new CompletableFuture[10]);
        // System.out.println(arr.length);
        CompletableFuture.allOf(arr).get();
    }
    public  static synchronized   int get( AtomicInteger atomicInteger){
        return atomicInteger.get();
    }

    @Override
    public void test2(){
        List<UserDo> list = new ArrayList<>();
        for(int i=0;i<=9999;i++){
            UserDo userDo = new UserDo();
            userDo.setUsername("test"+i);
            list.add(userDo);
        }
        this.saveBatch(list);
    }

    public void test3(int i){
        for(int a=1000*(i-1);a<1000*i;a++){
            UserDo userDo = new UserDo();
            userDo.setUsername("test"+a);
            userMapper.insert(userDo);
            log.info("成功插入第"+a+"条数据");
        }
    }


}
